package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/15
 */
@Component
public class PageData22 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){

        List<String> ytdDay = DateUtil.getYtdDay();

        Page22 page22 = new Page22();
        page22.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page22.setTitle(LogicUtil.getTitle(model,22));
        page22.setUtcAge(crmMemberService.getUtcAge(ytdDay.get(0),ytdDay.get(1)));
        List<TempBeen> utcArea = crmMemberService.getUtcArea(ytdDay.get(0), ytdDay.get(1));
        TempBeen other = getOther(utcArea);
        List<TempBeen> collect = utcArea.stream().skip(0).limit(10).collect(Collectors.toList());
        collect.add(other);
        collect.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        page22.setUtcArea(collect);
        List<TempBeen> utcGender = getUtcGender(ytdDay.get(0),ytdDay.get(1));
        page22.setUtcGender(utcGender);
        return JsonUtil.serialize(page22);
    }

    private List<TempBeen> getUtcGender(String s, String s1) {
        List<TempBeen> utcGender = crmMemberService.getUtcGender(s, s1);
        List<TempBeen> genders = new ArrayList<>();
        for (TempBeen tempBeen : utcGender) {
            if (tempBeen.getTempName().equals("未知")||tempBeen.getTempName().equals("男") ||tempBeen.getTempName().equals("女")){
                genders.add(tempBeen);
            }
        }
        return genders;
    }

    private TempBeen getOther(List<TempBeen> list){
        long sum = list.stream().skip(10).mapToLong(TempBeen::getTempCount).sum();
        TempBeen tempBeen = new TempBeen();
        tempBeen.setTempCount(sum);
        tempBeen.setTempName("其他");
        return tempBeen;
    }

    @Data
    private static class Page22{
        private String title;
        private String dataPeriod;
        private List<TempBeen> utcGender;
        private List<TempBeen> utcAge;
        private List<TempBeen> utcArea;
    }

}
